package com.example.demo.algorithm.delay_queue;

import lombok.SneakyThrows;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.DelayQueue;
import java.util.concurrent.TimeUnit;

/**
 * jdk自带的延时队列
 */
public class DelayQueueDemo {
    @SneakyThrows
    public static void main(String[] args) {
        Order order1 = new Order("order1",5, TimeUnit.SECONDS);
        Order order2 = new Order("order2",10, TimeUnit.SECONDS);
        Order order3 = new Order("order3",15, TimeUnit.SECONDS);

        DelayQueue<Order> delayQueue = new DelayQueue<>();
        delayQueue.put(order1);
        delayQueue.put(order2);
        delayQueue.put(order3);

        System.out.println("订单延迟队列开始时间：" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));

        while(delayQueue.size() > 0){
            Order task = delayQueue.poll();
            if(task != null){
                System.out.format("订单:{%s}被取消, 取消时间:{%s}\n", task.getName(), LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
            }
            Thread.sleep(1000);
        }
    }
}
